package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherWorkListDataBean;
import com.fancy.learncenter.bean.ListStudentDataBean;
import com.fancy.learncenter.bean.StudentRemoveDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.view.SelectGenderPopu;
import com.fancy.learncenter.ui.view.date.custom.DateTimePicker;
import com.fancy.learncenter.ui.view.date.custom.DateTimePickerDialog;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class EditStudentActivity extends BaseActivity {

    public static String STUDENT_KEY = "STUDENT_KEY";
    ListStudentDataBean.StudentsBean studentsBean;

    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.simpleDraweeView)
    SimpleDraweeView simpleDraweeView;
    @Bind(R.id.gender)
    TextView gender;
    @Bind(R.id.num)
    TextView num;
    @Bind(R.id.account)
    TextView account;
    @Bind(R.id.mother_mobile)
    EditText motherMobile;
    @Bind(R.id.father_mobile)
    EditText fatherMobile;
    @Bind(R.id.birthday)
    TextView birthday;
    @Bind(R.id.rootView)
    LinearLayout rootView;


    String classId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_student, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("学员信息");

        studentsBean = (ListStudentDataBean.StudentsBean) getIntent().getSerializableExtra(STUDENT_KEY);


        classId = getIntent().getStringExtra("classId");

        if (studentsBean != null) {
            name.setText(studentsBean.getName());

            simpleDraweeView.setImageURI(studentsBean.getImageUrl());

            gender.setText(studentsBean.getGender());

            num.setText(studentsBean.getStudentNo());

            account.setText(studentsBean.getMobile());

            fatherMobile.setText(studentsBean.getFatherPhone());

            motherMobile.setText(studentsBean.getMotherPhone());

            birthday.setText(studentsBean.getBirthday());
        }
    }


    private void editStudent() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", studentsBean.getId());

        if ("女".equals(gender.getText().toString())) {
            map.put("gender", "2");
        } else {
            map.put("gender", "1");
        }


        map.put("birthday", birthday.getText().toString());

        if (TextUtils.isEmpty(name.getText().toString())) {
            ToastUtil.show("请输入姓名");
        } else {
            map.put("name", name.getText().toString());
        }

        if (TextUtils.isEmpty(motherMobile.getText().toString())) {

        } else if (!Utils.isMobileNO(motherMobile.getText().toString())) {
            map.put("motherPhone", "");
            ToastUtil.show("请输入母亲的正确手机号码");
            return;
        } else {
            map.put("motherPhone", motherMobile.getText().toString());
        }


        if (TextUtils.isEmpty(fatherMobile.getText().toString())) {

        } else if (!Utils.isMobileNO(fatherMobile.getText().toString())) {
            map.put("fatherPhone", "");
            ToastUtil.show("请输入父亲的正确手机号码");
            return;
        } else {
            map.put("fatherPhone", fatherMobile.getText().toString());
        }


        HttpMehtod.getInstance().editStudent(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<CartoonTeacherWorkListDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherWorkListDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherWorkListDataBean>() {
                    @Override
                    public void success(CartoonTeacherWorkListDataBean dataBean) {
                        ToastUtil.show("保存成功");
                        finish();
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }

    private void removeStudent() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("studentId", studentsBean.getId());

        HttpMehtod.getInstance().removeStudent(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<StudentRemoveDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), StudentRemoveDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<StudentRemoveDataBean>() {
                    @Override
                    public void success(StudentRemoveDataBean dataBean) {

                        if (dataBean.isIsSuccess()) {
                            ToastUtil.show("删除成功");
                            finish();
                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }
        });

    }

    private void showTimeDialog(final TextView txv) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String time = sdf.format(new Date());
        DateTimePickerDialog dialog = DateTimePickerDialog.getInstance(time, "yyyy-MM-dd", DateTimePicker.DATE_PICKER);
        dialog.setListener(new DateTimePickerDialog.OnDateTimePickerListener() {
            @Override
            public void onDateTimePicked(long time) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                txv.setText(sdf.format(new Date(time)));
            }
        });
        dialog.show(getSupportFragmentManager(), DateTimePickerDialog.TAG + time);
    }

    SelectGenderPopu selectGenderPopu;

    @OnClick({R.id.gender, R.id.birthday, R.id.add_bt, R.id.delete_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.gender:
                if (selectGenderPopu == null) {
                    selectGenderPopu = new SelectGenderPopu(this, rootView, gender);
                }
                selectGenderPopu.showPopupWindow();
                break;
            case R.id.birthday:
                showTimeDialog(birthday);
                break;
            case R.id.add_bt:
                editStudent();
                break;
            case R.id.delete_bt:
                removeStudent();
                break;
        }
    }
}
